package com.ruoyi.web.controller.video;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.domain.video.VideoRecord;
import com.ruoyi.video.service.IVideoRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 視頻-目錄信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/video")
public class VideoController extends BaseController
{
    @Autowired
    private IVideoRecordService biz;

    /**
     * 获取目录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(VideoRecord dir)
    {
        startPage();
        List<VideoRecord> dirs = biz.selectList(dir);
        return getDataTable(dirs);
    }


    /**
     * 根据目录编号获取详细信息
     */
    @GetMapping(value = "/info/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(biz.findById(id));
    }

    /**
     * 新增目录
     */
    @Log(title = "视频管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody VideoRecord record)
    {
        if(record.getFile() == null || StringUtils.isEmpty(record.getFile().getFkey())) {
            return AjaxResult.error("no_file");
        }
        record.setCreateBy(getUsername());
        record.setCreateTime(DateUtils.getNowDate());
        return toAjax(biz.saveVideoRecord(record));
    }

    /**
     * 修改目录
     */
    @Log(title = "视频管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody VideoRecord record)
    {
        if(record.getFile() == null || StringUtils.isEmpty(record.getFile().getFkey())) {
            return AjaxResult.error("no_file");
        }
        record.setUpdateBy(getUsername());
        record.setUpdateTime(DateUtils.getNowDate());
        return toAjax(biz.updateVideoRecord(record));
    }

    /**
     * 删除目录
     */
    @Log(title = "视频管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        VideoRecord _r = biz.selectByPrimaryKey(id);
        return toAjax(biz.delete(_r));
    }
}
